// 连接 mongodb 数据库服务
const mongoose = require('mongoose')
const url = 'mongodb://localhost:27017/comment' //本地mongodb连接地址
// 开始连接  serverSelectionTimeoutMS:1000 ==>设置超时时间1秒
mongoose.connect(url, { serverSelectionTimeoutMS: 1000 })
  .then((db, never) => {
    console.log('mongodb link ok')
    console.log('db = ',db)
    console.log('never = ',never)
  }).catch((reason) => {
  console.error('mongodb link error==>>\nreason = ', reason)
})


// 监听超时事件
mongoose.connection.on('serverSelectionTimeout', () => console.error('服务器选择超时'))
// 监听其他事件以便调试
mongoose.connection.on('error', (error) => console.error('连接错误:', error))
mongoose.connection.on('open', () => console.log('数据库连接成功'))
mongoose.connection.on('close', () => console.log('数据库连接断开'))

module.exports = mongoose
